package com.seatch.msauth.config;

import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySource;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class MyConfiguration implements ApplicationContextInitializer {

    public void initialize(ConfigurableApplicationContext applicationContext) {
        ConfigurableEnvironment environment = applicationContext.getEnvironment();
        // 获取 PropertySource
        MutablePropertySources propertySources = environment.getPropertySources();
        // 定义一个新的 PropertySource 放在首位
        propertySources.addFirst(createPropertySource());
    }

    private PropertySource createPropertySource() {
        Map<String, Object> source = new HashMap<String, Object>();
        source.put("name", "AlanShelby");
        source.put("server.port",8999);
        PropertySource propertySource = new MapPropertySource("my-property-source", source);

        return propertySource;
    }
}